/*
  This file is part of jTopaz.
  http://gcl.cis.udel.edu/projects/jtopaz
  Copyright (C) 2009 University of Delaware

  jTopaz is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  jTopaz is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of                                                        
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with jTopaz.  If not, see <http://www.gnu.org/licenses/>.
*/

package jTopaz;

import org.globus.ftp.GridFTPClient;
import org.globus.ftp.FTPClient;
import java.io.File;
import org.globus.ftp.FileRandomIO;
import java.io.RandomAccessFile;
import java.util.TimerTask;

/**
   The actual execution thread for a download transfer.
*/
public class DownloadThread extends Thread {

    private GridFTPClient client;
    private FTPClient AnonClient;
    private File localFile;
    private String remoteFile;
    private long size;
    private boolean complete;

    /**
       Creates a new DownloadThread object.
       @param client the GridFTPClient object that will do the actual transfer.
       @localFile the file that will be downloaded to.
       @remoteFile the name of the file that will serve as the source.
       @size the size of the remote file.
    */
    public DownloadThread(GridFTPClient client, File localFile, String remoteFile, long size) {

	this.client = client;
	this.localFile = localFile;
	this.remoteFile = remoteFile;
	this.size = size;
	complete = false;

    }
    public DownloadThread(FTPClient AnonClient, File localFile, String remoteFile, long size) {
	this.AnonClient = AnonClient;
	this.localFile = localFile;
	this.remoteFile = remoteFile;
	this.size = size;
	complete = false;
    }

    /**
       The actual execution.  calls GridFTPClient.extendedGet();
    */
    public void run() {
	
	try {
		    
	    RandomAccessFile raFile = new RandomAccessFile(localFile, "rw");
	    FileRandomIO sink = new FileRandomIO(raFile);
	    MyMarkerListener listener = new MyMarkerListener();
		
	    //I want to remove this, but for now, keep it...
	    //defeats the whole purpose of extendedGet
	    if(client!=null){
		System.out.println(client);
		//client.setPassive();
		//client.setLocalActive();
		//client.extendedGet(remoteFile, size, sink, listener);
		System.out.println("remotefile=" + remoteFile + " localFile=" + localFile);
		client.get(remoteFile, localFile);
		complete = true;
	    }
	    else {
		System.out.println(AnonClient);
		AnonClient.get(remoteFile, localFile);
		complete=true;
	    }
	}
	
	catch (Exception e) {
	    System.out.println("exception in DownloadThread run()");
	    System.out.println(e);
	    e.printStackTrace();
	}
    }
    
    /**
       Returns whether or not the transfer has completed.
    */
    public boolean transferComplete() {
	
	//System.out.println("tranferComplete returns: " + complete);
	return complete;
    }
    
    public void cancel() {
	complete = true;
	try {
	    System.out.println("Don't close the connection");
	    if(client!=null){
		client.abort();
	    }
	    else {
		AnonClient.abort();
	    }
	    //client.close();
	    //complete = true;
	}
	catch (Exception e) {
	    System.out.println("abort failed?");
	    System.out.println(e);
	}
    }
}
